<?php
namespace Common\Model;

use Common\Util\BaseData;
use Think\Model;

class UserModel extends Model
{
    protected $tableName = 'user';

    const TIP_TYPE_NOW = 1;
    const TIP_TYPE_15_MINUTE = 2;
    const TIP_TYPE_ONE_HOUR = 3;
    const TIP_TYPE_3_HOUR = 3;

    const TIP_TYPE_MAPS = [
        self::TIP_TYPE_NOW => '立即提醒',
        self::TIP_TYPE_15_MINUTE => '15分钟',
        self::TIP_TYPE_ONE_HOUR => '1小时',
        self::TIP_TYPE_3_HOUR => '3小时',
    ];

    const IS_VIP = 1;
    const NOT_VIP = 0;


    /**
     * 获取用户详情
     *
     * @param int $user_id
     * @return mixed
     */
    public function detail($user_id = 0)
    {
        $user_id = intval($user_id);
        return $this->where(['id' => $user_id])->find();
    }

    /**
     * 根据用户openid获取用户信息
     *
     * @param        $open_id
     * @param string $field
     * @return bool|mixed|string
     */
    public function getUserInfoByUserId($user_id = 0, $field = '')
    {
        $user_id = intval($user_id);
        if (!$user_info = $this->where(['id' => $user_id])->find()) {
            return false;
        }
        $user_info['user_headimg'] = $user_info['user_headimg'] ? $user_info['user_headimg'] : "http://xcx.tfxiaochengxu.com/2018-06-15_5b235fa500726.png";
        if (!empty($field)) {
            return $user_info[$field] ? $user_info[$field] : '';
        }
        return $user_info ? $user_info : false;
    }

    /**
     * 根据用户openid获取用户信息
     *
     * @param        $open_id
     * @param string $field
     * @return bool|mixed|string
     */
    public function getUserInfoByOpenId($open_id, $field = '')
    {
        if (!$user_info = $this->where("user_openid='$open_id'")->find()) {
            return false;
        }
        $user_info['user_headimg'] = $user_info['user_headimg'] ? $user_info['user_headimg'] : "http://xcx.tfxiaochengxu.com/2018-06-15_5b235fa500726.png";
        if (!empty($field)) {
            return $user_info[$field] ? $user_info[$field] : '';
        }
        return $user_info ? $user_info : false;
    }

    /**
     * 根据用户openid获取用户信息
     *
     * @param        $open_id
     * @param string $field
     * @return bool|mixed|string
     */
    public function getUserInfoByAlipayOpenId($open_id, $field = '')
    {
        if (!$user_info = $this->where("user_alipay_openid='$open_id'")->find()) {
            return false;
        }
        $user_info['user_headimg'] = $user_info['user_headimg'] ? $user_info['user_headimg'] : "http://xcx.tfxiaochengxu.com/2018-06-15_5b235fa500726.png";
        if (!empty($field)) {
            return $user_info[$field] ? $user_info[$field] : '';
        }
        return $user_info ? $user_info : false;
    }

    /**
     * 根据用户手机号码获取用户信息
     *
     * @param $user_id
     * @return array|mixed
     */
    public static function getUserInfoByPhone($user_phone, $field = '')
    {
        $t_user = M("user");
        $user_phone = trim($user_phone);
        if (!$user_info = $t_user->where(['user_true_phone' => $user_phone])->find()) {
            return false;
        }
        $user_info['user_headimg'] = $user_info['user_headimg'] ? $user_info['user_headimg'] : "http://xcx.tfxiaochengxu.com/2018-06-15_5b235fa500726.png";
        if (!empty($field)) {
            return $user_info[$field] ? $user_info[$field] : '';
        }
        return $user_info ? $user_info : false;
    }

    /**
     * 根据用户$user_token获取用户信息
     *
     * @param        $user_token
     * @param string $field
     * @return bool|mixed|string
     */
    public function getUserInfoByUserToken($user_token = '')
    {
        if (!$user_info = $this->where(['user_token' => $user_token])->find()) {
            return false;
        }
        $user_info = $this->fomartUserInfo($user_info);
        return $user_info ? $user_info : false;
    }

    /**
     * 格式化用户信息
     *
     * @param $user_info
     * @return mixed
     */
    public function fomartUserInfo($user_info)
    {
        return $user_info;
    }

    /**
     * 生成用户登陆token
     *
     * @param int $user_id
     * @param string $user_token
     * @return bool|string
     */
    public function updateUserLoginToken($user_id = 0)
    {
        if (!$user_info = $this->getUserInfoByUserId($user_id)) {
            return false;
        }
        $user_token = sha1(uniqid(md5(microtime(true)), true) . $user_id);
        $s_u_data['user_token'] = $user_token;
        $s_u_data['user_token_expires'] = time() + 3600 * 24 * 30;
        $s_u_data['update_time'] = time();
        if ($this->where(['id' => $user_id])->save($s_u_data)) {
            return $user_token;
        } else {
            return false;
        }
    }

    /**
     * 新用户注册逻辑处理
     *
     * @param int $user_id
     * @return bool
     */
    public function userRegAction($user_id = 0)
    {
        $app_setting = BaseData::getAppSetting();
        if (!$user_info = $this->detail($user_id)) {
            return false;
        }
        return true;
    }

    /**
     * 更新用户团队路径
     *
     * @param $user_id
     * @param $referrer_user_id
     * @return false|mixed|string
     */
    public function updateUserPath($user_id = 0, $referrer_user_id = 0)
    {
        if (!$user_info = $this->detail($user_id)) {
            return false;
        }
        if ($user_info['referrer_user_id'] && $user_info['referrer_user_id'] > 0) {
            return false;
        }
        if ($referrer_user_id >= $user_id) {
            return false;
        }

        if ($referrer_user_id == 0 || empty($referrer_user_id)) {
            $this->where(['id' => $user_id])->save([
                'user_path' => $user_id,
                'update_time' => time(),
            ]);
        } else {
            if (!$ref_user_info = $this->detail($referrer_user_id)) {
                $this->where(['id' => $user_id])->save([
                    'user_path' => $user_id,
                    'update_time' => time(),
                ]);
            } else {
                $this->where(['id' => $user_id])->save([
                    'referrer_user_id' => $referrer_user_id,
                    'user_path' => $ref_user_info['user_path'] . "," . $user_id,
                    'update_time' => time(),
                ]);
            }
        }
        return true;
    }

    /**
     * 更新用户VIP
     *
     * @param $user_id
     * @param $referrer_user_id
     * @return false|mixed|string
     */
    public function updateUserVip($user_id = 0, $vip_type = 0, $vip_day = 0, $send_type = 0)
    {
        if (!$user_info = $this->detail($user_id)) {
            return false;
        }
        if ($vip_type == 1) {
            //			有效期会员
            if ($user_info['vip_type'] == 1) {
                $end_time = $user_info['vip_end_time'] + 3600 * 24 * $vip_day;
            } else {
                $end_time = time() + 3600 * 24 * $vip_day;
            }
        } else {
            //			普通会员
            $end_time = 0;
        }
        $log = [
            'user_id' => $user_info['id'],
            'open_type' => $send_type,
            'update_vip_end_time' => $end_time,
            'remark' => $this->getTypeName($send_type),
            'update_time' => time(),
        ];
        $addLog = M("user_vip_log")->add($log);
        $saveUser = M("user")->where(['id' => $user_id])->save([
            'vip_type' => $vip_type,
            'is_vip' => 1,
            'vip_end_date' => date("Y-m-d H:i:s", $end_time),
            'vip_end_time' => $end_time,
        ]);
        if ($addLog && $saveUser) {
            return true;
        } else {
            return false;
        }
    }

    public function getTypeName($type)
    {
        $remark_array = [
            '1' => '支付开通',
            '2' => '激活码开通',
            '3' => '邀请奖励续',
            '4' => ' 注册赠送',
        ];
        return $remark_array[$type];
    }
}

?>
